// pages/feedback/feedback.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    feedback: '',
    images: [],
    contact: '',
    isDarkMode: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const isDark = wx.getStorageSync('isDarkMode');
    this.setData({
      isDarkMode: typeof isDark === 'boolean' ? isDark : (getApp().globalData.isDarkMode || false)
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  updateTheme(isDark) {
    this.setData({ isDarkMode: isDark });
  },

  onInput(e) {
    this.setData({ feedback: e.detail.value });
  },

  onContactInput(e) {
    this.setData({ contact: e.detail.value });
  },

  chooseImage() {
    wx.chooseImage({
      count: 3 - this.data.images.length,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        this.setData({
          images: this.data.images.concat(res.tempFilePaths)
        });
      }
    });
  },

  previewImage(e) {
    const index = e.currentTarget.dataset.index;
    wx.previewImage({
      current: this.data.images[index],
      urls: this.data.images
    });
  },

  removeImage(e) {
    const index = e.currentTarget.dataset.index;
    const images = this.data.images.slice();
    images.splice(index, 1);
    this.setData({ images });
  },

  submitFeedback() {
    if (!this.data.feedback.trim() && this.data.images.length === 0) {
      wx.showToast({ title: '请填写反馈内容或上传图片', icon: 'none' });
      return;
    }
    // 可在此处将contact一并提交到后端
    wx.showToast({ title: '感谢您的反馈！', icon: 'success' });
    this.setData({ feedback: '', images: [], contact: '' });
  }
})